﻿using Microsoft.Owin;
using Microsoft.Owin.Security.ActiveDirectory;
using Owin;
using System.Configuration;



[assembly: OwinStartup(typeof(HP.HPTRIM.ADFSOAuth.Startup))]
namespace HP.HPTRIM.ADFSOAuth
{

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {

            string metadata = ConfigurationManager.AppSettings["ida:AdfsMetadataEndpoint"];
            string audience = ConfigurationManager.AppSettings["ida:Audience"];
            string tenant = ConfigurationManager.AppSettings["ida:Tenant"];

            if (!string.IsNullOrEmpty(audience))
            {
                if (!string.IsNullOrEmpty(tenant))
                {
#pragma warning disable 618
                    app.UseWindowsAzureActiveDirectoryBearerAuthentication(
                        new WindowsAzureActiveDirectoryBearerAuthenticationOptions
                        {
                            Audience = audience,
                            Tenant = tenant

                        });
                }
                else if (!string.IsNullOrEmpty(metadata))
                {
#pragma warning disable 618
                    app.UseActiveDirectoryFederationServicesBearerAuthentication(
                        new ActiveDirectoryFederationServicesBearerAuthenticationOptions
                        {
                            MetadataEndpoint = metadata,
                            AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,
                            TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters()
                            {
                                ValidAudience = audience
                            }
                        });
                }
            }

        }
    }
}
